package com.information.manage.basic.common.token.interceptor;

import com.google.common.collect.Lists;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Component
public class LoginAdapter /** extends WebMvcConfigurerAdapter **/ implements WebMvcConfigurer {

    //解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("Content-Type", "X-Requested-With", "accept,Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", "token")
                .allowedMethods("*")
                .allowedOrigins("*")
                //是否允许使用cookie
                .allowCredentials(true);
    }

    @Resource
    private LoginInterceptor loginInterceptor;

    // 这个方法是用来配置静态资源的，比如html，js，css，等等
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    // 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //忽略地址,不需要校验jwt的url写在excludePathPatterns里
        List<String> excludePathPatterns = Lists.newArrayList("/error", "/favicon.ico", "/sys/user/login", "/swagger-resources/**"
                , "/v2/api-docs/**", "/v2/api-docs-ext", "/webjars/**", "/doc.html", "/image/**");
        //addPathPatterns是表明拦截哪些请求 excludePathPatterns是对哪些请求不做拦截
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(excludePathPatterns);
    }

    //注册拦截器 不同的拦截器拦截不同的请求 做不同的处理
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        super.addInterceptors(registry);
//        registry.addInterceptor(webIterceptor()).addPathPatterns("/web/**");
//        registry.addInterceptor(apiIterceptor()).addPathPatterns("/api/**");
//        registry.addInterceptor(clientIterceptor()).addPathPatterns("/client/**");
//        registry.addInterceptor(rpClientIterceptor()).addPathPatterns("/client/rp/**");
//    }

}